package com.gjzt.generator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl;

/**
 * Created by Administrator on 2016/3/17.
 */
public class BatisJavaTypeResolver extends JavaTypeResolverDefaultImpl {

    public BatisJavaTypeResolver() {
        super();
    }

    @Override
    public String calculateJdbcTypeName(IntrospectedColumn introspectedColumn) {
        String answer = super.calculateJdbcTypeName(introspectedColumn);
//        if ("INTEGER".equals(answer)) {
//            answer = "int";
//        } else if ("LONG".equals(answer)) {
//            answer = "long";
//        } else if ("DOUBLE".equals(answer)) {
//            answer = "double";
//        } else if ("FLOAT".equals(answer)) {
//            answer = "float";
//        }
        return answer;
    }

    @Override
    public FullyQualifiedJavaType calculateJavaType(IntrospectedColumn introspectedColumn) {
        FullyQualifiedJavaType answer = super.calculateJavaType(introspectedColumn);
        String type = answer.getShortName();
        if ("Integer".equals(type) || "Long".equals(type) || "Double".equals(type) || "Float".equals(type)) {
            answer = new NumFieldJavaType(answer.getFullyQualifiedName());
        }
        return answer;
    }

    class NumFieldJavaType extends FullyQualifiedJavaType {

        /**
         * Use this constructor to construct a generic type with the specified
         * type parameters
         *
         * @param fullTypeSpecification
         */
        public NumFieldJavaType(String fullTypeSpecification) {
            super(fullTypeSpecification);
        }

        @Override
        public String getShortName() {
            String answer = super.getShortName();
            if ("Integer".equals(answer)) {
                answer = "int";
            } else if ("Long".equals(answer)) {
                answer = "long";
            } else if ("Double".equals(answer)) {
                answer = "double";
            } else if ("Float".equals(answer)) {
                answer = "float";
            }
            return answer;
        }

        @Override
        public String getFullyQualifiedName() {
            String answer = super.getFullyQualifiedName();
//            if ("Integer".equals(answer)) {
//                answer = "int";
//            } else if ("Long".equals(answer)) {
//                answer = "long";
//            } else if ("Double".equals(answer)) {
//                answer = "double";
//            } else if ("Float".equals(answer)) {
//                answer = "float";
//            }
            return answer;
        }

        @Override
        public String toString() {
            return super.toString();
        }
    }
}
